package com.idp.web.controller;

import com.idp.common.base.BaseController;
import com.idp.common.base.BaseResult;
import com.idp.common.constants.CommonConstants;
import com.idp.common.constants.Constants;
import com.idp.common.model.UploadModel;
import com.idp.common.persistence.Page;
import com.idp.common.util.UploadUtils;
import com.idp.common.util.ValidateUtils;
import com.idp.web.dto.NurseShowDTO;
import com.idp.web.dto.PatientDTO;
import com.idp.web.dto.PatientListDTO;
import com.idp.web.entity.Nurse;
import com.idp.web.entity.Patient;
import com.idp.web.service.IPatientService;
import com.idp.web.vo.NurseSearchVo;
import com.idp.web.vo.PatientSearchVo;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 患者controller
 *
 * @author xubinren
 * @version 1.0
 * @date 2018/3/6
 */
@Controller
@RequestMapping("/patient")
public class PatientController extends BaseController {

    private Logger logger = Logger.getLogger(PatientController.class);

    @Resource
    private IPatientService patientService;

    /**
     * 初始化查询页面
     *
     * @return
     */
    @RequestMapping("/init")
    public String init() {
        return "backstage/patient/patientSearch";
    }

    /**
     * 分页查询
     *
     * @param patientSearchVo
     * @param page
     * @param request
     * @return
     */
    @RequestMapping("/list")
    public String list(PatientSearchVo patientSearchVo, Page<PatientListDTO> page, HttpServletRequest request) {
        try {
            request.setAttribute("page", patientService.findByPage(patientSearchVo, page));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return "backstage/patient/patientList";
    }

    /**
     * 新增修改页面初始化
     *
     * @param id
     * @param request
     * @return
     */
    @RequestMapping("/patient")
    public String patient(String id, HttpServletRequest request) {
        try {
            if (ValidateUtils.isNotEmpty(id)) {
                Patient patient = patientService.getById(id);
                request.setAttribute("patient", patient);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return "backstage/patient/patient";
    }

    /**
     * 保存或修改患者信息
     *
     * @param request 患者信息
     * @return String
     */
    @RequestMapping("/save")
    @ResponseBody
    public String save(HttpServletRequest request) {
        JSONObject json = new JSONObject();
        try {
            Patient patient = new Patient();
            UploadModel model = new UploadModel(UploadUtils.getServerUploadBasePath(UploadUtils.PATH_IMAGES),
                    UploadModel.IMAGES, patient);
            UploadUtils.uploadFileSealedObject(request, model);
            if (ValidateUtils.isNotEmpty(patient.getId())) {
                // 修改

                String strMiddle = request.getParameter(CommonConstants.AVATAR_URL);
                if (StringUtils.isEmpty(patient.getAvatarUrl()) && StringUtils.isNotEmpty(strMiddle)){
                    Patient patient1 = patientService.getById(patient.getId());
                    patient.setAvatarUrl(patient1.getAvatarUrl());
                }
                patientService.update(patient,getUserId());
            }else {
                // 新增

                patientService.add(patient,getUserId());
            }
            json.put("result", "save_success");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            json.put("result", "save_fail");
        }
        return json.toString();
    }

    /**
     * 查看页面初始化
     *
     * @param id      患者id
     * @param request HttpServletRequest
     * @return String
     */
    @RequestMapping("/show")
    public String show(String id, HttpServletRequest request) {
        if (ValidateUtils.isNotEmpty(id)) {
            Patient patient = patientService.getById(id.trim());
            PatientDTO patientDTO = new PatientDTO();
            BeanUtils.copyProperties(patient,patientDTO);
            request.setAttribute("patient", patientDTO);
        } else {
            logger.info("参数错误");
        }
        return "backstage/patient/patientShow";
    }

    /**
     * 根据患者id删除对应的患者信息
     *
     * @param id 患者id
     * @return String
     */
    @RequestMapping("/delete")
    @ResponseBody
    public String delete(String id) {
        JSONObject json = new JSONObject();
        boolean bl = patientService.delete(id);
        if (bl){
            json.put("result", "delete_success");
        }else {
            json.put("result", "delete_fail");
        }
        return json.toString();
    }

    /**
     * 根据医生id修改医生的账户状态
     *
     * @param id 医生id
     * @return String
     */
    @RequestMapping("/changeStatus")
    @ResponseBody
    public String changeStatus(String id) {
        JSONObject json = new JSONObject();
        if (StringUtils.isEmpty(id)){
            json.put("result", Constants.RESPONSE_MESSAGE_400);
        }else {
            BaseResult baseResult = patientService.changeStatus(id,getUserId());
            json.put("result", baseResult.getMessage());;
        }
        return json.toString();
    }
}
